<?php
/**
 * Created by PhpStorm.
 * User: wangp
 * Date: 2016/7/7
 * Time: 20:28
 */

namespace Home\Controller;
include_once __DIR__ . '/vendor/autoload.php';
use EasyWeChat\Foundation\Application;
use EasyWeChat\Payment\Order;
class wechatpay extends \Home\Controller\BaseController{
    function wepay($attributes){

        $options = [
            'debug' => true,
            'app_id' => 'wx73ce0c7fa5af1828',    //app_id
            'secret' => '4387c33d9a29182e713286c8f48fcf89',    //app_secret
            'token' => '',  //token
            'log' => [
                'level' => 'debug',
                'file'  => 'abc.log',
            ],
            'guzzle' => [
                'timeout' => 3.0,// 超时
                'verify' => false,
            ],
            'oauth' => [
                'scopes' => ['snsapi_base'],
                'callback' => 'wx/oauth_callback.php',
            ],
            'payment' => [
                'merchant_id'        => '1353103702',   //商户ID
                'key'                => 'ksJJKzMCYXiNO69Ud4P086z0hobLMkyj', //商户key
                'cert_path'          => __DIR__ . '/../cert1/apiclient_cert.pem', // 证书
                'key_path'           => __DIR__ . '/../cert1/apiclient_key.pem',      // 证书
                'notify_url'         => 'http://121.14.38.35/payback.php',       // 回调地址
            ]
        ];

        $app = new Application($options);

        $payment = $app->payment;

        $order = new Order($attributes);
        $result = $payment->prepare($order);

        $prepayId = '';
        if ($result->return_code == 'SUCCESS' && $result->result_code == 'SUCCESS') {
            $prepayId = $result->prepay_id;
        }
        if( $prepayId ){
            $json = $payment->configForPayment($prepayId,"1353103702");
        }
        return $json;
    }
    function payback(){

        $options = [
            'debug' => true,
            'app_id' => 'wx73ce0c7fa5af1828',    //app_id
            'secret' => '4387c33d9a29182e713286c8f48fcf89',    //app_secret
            'token' => '',  //token
            'log' => [
                'level' => 'debug',
                'file'  => 'abc.log',
            ],
            'guzzle' => [
                'timeout' => 3.0,// 超时
                'verify' => false,
            ],
            'oauth' => [
                'scopes' => ['snsapi_base'],
                'callback' => 'wx/oauth_callback.php',
            ],
            'payment' => [
                'merchant_id'        => '1353103702',   //商户ID
                'key'                => 'ksJJKzMCYXiNO69Ud4P086z0hobLMkyj', //商户key
                'cert_path'          => __DIR__ . '/../cert1/apiclient_cert.pem', // 证书
                'key_path'           => __DIR__ . '/../cert1/apiclient_key.pem',      // 证书
                'notify_url'         => 'http://121.14.38.35/payback.php',       // 回调地址
            ]
        ];

        $app = new Application($options);
        $db = M("Invest");
        $payment = $app->payment;
        $response = $payment->payment->handleNotify(function($notify, $successful) use ($db){
            // 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单
            $order_no = $notify->out_trade_no;
            $order = $db->where(array("order_no"=>$order_no))->save(array('paysuccess'=>'1',"status"=>'3'));
            
            return true;
        });
        $response->send();
    }
}
